<?php
/*
Created by:- aparna agarwal
Created on:- 6th july 2004
Description :- function to validate and change password 
*/

##########change password##################

function change_password($user_table,$user_field_value,$OldPassword,$NewPassword,$user_field='username',$password_field='password')
{
 /*
 $table name= table name where update query wil run 
 $user_field_valu = key field value 
 $OldPassword = Old Password value to be compared
 $NewPassword = New Password value to be set
 $user_field = table Key Field name for example Loyal_username or loyal_email , enter '' if you use 'username' field in database table
 $password_field = Password Field name for example Loyal_password , enter '' if you use 'password' field in database table
 */

 // setting the value of return variables

 define("UPDATE_SUCCESS","1");
 define("INVALID_USER","0");
 define("SQL_ERROR","-1");
 
 $str ="select * from $user_table where ".$user_field." = '".$user_field_value."' and ".$password_field." = '".$OldPassword."'";
 
 $chkpass=mysql_query($str);
 
 if(mysql_error())
  return SQL_ERROR;
 
 if(mysql_num_rows($chkpass)>0)
 {
	  $upd_str ="update $user_table set ".$password_field." = '".$NewPassword."' where ".$user_field." = '".$user_field_value."'";
	  
	  mysql_query($upd_str);
		return UPDATE_SUCCESS;
 
 }else{
  return INVALID_USER;
 }
 
}



function forgot_password($table_name,$key_fld_val,$key_fld_name='username',$where_condition='',$user_name_fld='username',$user_pass_fld='password')
{

/*
 $table name= table name where select query wil run 
 $key_fld_val= key field value used in table 
 $key_fld_name = key field name used in table 
 $where_condition = where condition
 $user_name_fld = get the username field used in  table 
 $user_pass_fld = get the password field used in  table 
 retruns username,password
*/

// setting the default value of return field

	define("UPDATE_SUCCESS","1");
	define("INVALID_USER","0");
	define("SQL_ERROR","-1");

	$str ="select $user_name_fld,$user_pass_fld from $table_name where ".$key_fld_name." = '".$key_fld_val."' $where_condition ";

	$row=mysql_query($str);

	if(mysql_error())
		return SQL_ERROR;
	
	if(mysql_num_rows($row)>0)
	{
		$result=mysql_fetch_array($row);
		//return pasword
		return $result;
	}
	else
	{
		return INVALID_USER;
	}

}


function forgot_password_user($table_name,$key_fld_val,$key_fld_name,$where_condition='',$user_name_fld='user_name',$user_pass_fld='pass_word')
{

/*
 $table name= table name where select query wil run 
 $key_fld_val= key field value used in table 
 $key_fld_name = key field name used in table 
 $where_condition = where condition
 $user_name_fld = get the username field used in  table 
 $user_pass_fld = get the password field used in  table 
 retruns username,password
*/

// setting the default value of return field

//$where_condition=" and user_name != '' "; 

	define("UPDATE_SUCCESS","1");
	define("INVALID_USER","0");
	define("SQL_ERROR","-1");

	$str ="select $user_name_fld,$user_pass_fld from $table_name where ".$key_fld_name." = '".$key_fld_val."' $where_condition ";

	
	$row=mysql_query($str);

	if(mysql_error())
		return SQL_ERROR;
	
	if(mysql_num_rows($row)>0)
	{
		$result=mysql_fetch_array($row);
		if($result['user_name']=="")
		{
			return "nouser";
		}
		//return pasword
		return $result;
	}
	else
	{
		return INVALID_USER;
	}

}

?>
